package cn.bob.product.controller;

import cn.bob.basic.jwt.LoginContext;
import cn.bob.order.domain.OrderProduct;
import cn.bob.order.query.OrderProductQuery;
import cn.bob.org.domain.Employee;
import cn.bob.org.mapper.EmployeeMapper;
import cn.bob.org.service.IEmployeeService;
import cn.bob.pet.domain.Pet;
import cn.bob.pet.query.SearchMasterMsgQuery;
import cn.bob.product.domain.ProductStaycheck;
import cn.bob.product.service.IProductService;
import cn.bob.product.domain.Product;
import cn.bob.product.query.ProductQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //只能查询当前登录人的店铺中有的服务
        Employee emp = employeeMapper.findByLogid(logininfo.getId());
        product.setShopId(emp.getShopId());
        try {
            if( product.getId()!=null)
                productService.update(product);
            else{
                //在添加时默认添加当前登录人的店铺
                productService.add(product);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.delById(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id")Long id)
    {
        return productService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> list(){

        return productService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Product> json(@RequestBody ProductQuery query, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //只能查询当前登录人的店铺中有的服务
        Employee emp = employeeMapper.findByLogid(logininfo.getId());
        query.setShopId(emp.getShopId());
        return productService.queryByPage(query);
    }

    @GetMapping("/bydetail/{id}")
    public Product bydetail(@PathVariable("id") Long id){
        return productService.bydetail(id);
    }

    //批量删除
    @PostMapping("/dels")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        productService.batchDel(ids);
        return AjaxResult.me().success();
    }

    //上架
    @PostMapping("/onsale")
    public AjaxResult onsalePet(@RequestBody List<Long> ids){
        //创建html静态可能有问题
         productService.onsaleProduct(ids);
        return AjaxResult.me().success();
    }
    //下架
    @PostMapping("/offsale")
    public AjaxResult offsalePet(@RequestBody List<Long> ids){
        productService.offsaleProduct(ids);
        return AjaxResult.me().success();
    }

    //已完成数据
    @PostMapping("/productfinish")
    public PageList<ProductStaycheck> finish(@RequestBody OrderProductQuery query, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //只能查询当前登录人的店铺中有的服务
        Employee emp = employeeMapper.findByLogid(logininfo.getId());
        query.setState(1);
        query.setShopId(emp.getShopId());
        return productService.finish(query);
    }
}
